<?php

/**
 * This is the model class for table "wxuser_bonus_use".
 *
 * The followings are the available columns in table 'wxuser_bonus_use':
 * @property string $id_wxuser_bonus_use
 * @property string $id_wxuser
 * @property string $id_merchant
 * @property string $id_tradedetail
 * @property string $bonus
 * @property string $wxuser_bonus_beforedone
 * @property string $wxuser_bonus_afterdone
 * @property string $createtime
 */
class WxuserBonusUse extends ActiveRecord
{
   public $merchant_name;
    
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Wxuser the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'wxuser_bonus_use';
	}

    //获取记录
    public function getUsedBonusByUid($wxuser_id,$offset=0,$limit=10)
    {
        return $this->findAllBySql('
                SELECT wxuser_bonus_use.*, merchant.name merchant_name 
	            FROM wxuser_bonus_use  
	            LEFT JOIN merchant ON wxuser_bonus_use.id_merchant = merchant.id_merchant 
	            WHERE id_wxuser = :id_wxuser 
                ORDER BY createtime DESC
                Limit :offset,:limit
            ', array(
            ':id_wxuser' => $wxuser_id,
            ':offset' => $offset,
            ':limit' => $limit,
        ));
    }
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('merchant_name, id_wxuser_bonus_use, id_wxuser, id_merchant, id_tradedetail, bonus, wxuser_bonus_beforedone, wxuser_bonus_afterdone, createtime', 'safe'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
			
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
		);
	}

	public function getWxuser()
	{
		return Wxuser::model()->findByAttributes(array(
			'id_wxuser' => $this->id_wxuser
		));
	}

	public function getMerchant()
	{
		return Merchant::model()->findByAttributes(array(
			'id_merchant' => $this->id_merchant
		));
	}
	
	
}